﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VentaElectrodomesticos.Contrato;
using VentaElectrodomesticos.Negocio;
using VentaElectrodomesticos.Datos;

namespace VentaElectrodomesticos.GUI.Base
{
    public partial class BaseEdicion<T,W,Z> : BaseValidaciones
        where T : BaseNegocio<W,Z>, new() 
        where W : BaseDAO<Z>, new() 
        where Z : Entity, new()
    {
        private Z entity;
        public bool edit {get;set;}

        public virtual Z Entity 
        {
            get 
            {
                return entity;
            }
            set
            {
                entity = value;
                this.setValues();
            }
        }
        protected virtual T Negocio { get; set; }

        protected virtual void initialize() {
            this.Errores = new List<string>();
        }

        private void _intiliaze()
        {
            this.Negocio = new T();
            this.DialogResult = DialogResult.Cancel;
        }

        public BaseEdicion()
        {            
            InitializeComponent();
            this._intiliaze();
        }

        public virtual void changeLayout(bool edit) {
            this.edit = edit;
        }

        protected void cancel()
        {
            this.entity = new Z();
            this.Hide();
        }

        protected override void doSave()
        {
            this.getValues();
            try
            {
                if (this.Negocio.save(this.entity))
                {
                this.DialogResult = DialogResult.OK;
                MessageBox.Show("Guardado Correcto", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Se produjo un error al guardar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                this.Close();
            }
            catch(NegocioException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (DatosException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch(Exception) {
                MessageBox.Show("Error inesperado. Ponganse en contacto con el administrador del sistema ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btCancelar_Click(object sender, EventArgs e)
        {
            this.cancel();
        }

        private void btAceptar_Click(object sender, EventArgs e)
        {
            this.save();
        }

        protected virtual void clear() {}

        protected virtual void getValues() {}

        protected virtual void setValues() {}

        protected virtual void BaseEdicion_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.cancel();
            if (this.Tag != null)
                ((Form)this.Tag).Focus();
        }

        private void BaseEdicion_Load(object sender, EventArgs e)
        {
            this.initialize();
        }
    }
}
